package com.linln.modules.member.repository;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.constant.StatusConst;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.system.repository.BaseRepository;


/**
 * @author hct
 * @date 2019/09/12
 */
public interface MemberAgentRepository extends BaseRepository<MemberAgent, Long>, JpaSpecificationExecutor<MemberAgent> {
	
	 public MemberAgent getByMemberIdAndArea(Long memberId,String area);

	public MemberAgent getByArea(String area);

	    @Modifying
	    @Transactional
 	    @Query(value="update member_agent  set curmoney = ?1  where id = ?2 and status = " + StatusConst.OK,nativeQuery = true)
	    public Integer updateCurmoney(Double money, Long id);
	    
	    @Modifying
	    @Transactional
 	    @Query(value="update member_agent  set status = ?1  where id = ?2 and status = 0 ",nativeQuery = true)
	    public Integer updateAgentStatus(Integer agentStatus,Long id);

	    
	    /**
		  * 确认已经支付，修改状态
		  * @param orderId
		  * @return
		  */
	    @Modifying
	    @Transactional
	    @Query("update MemberAgent set payStatus =1  where orderId = ?1 and payStatus=0 ")
	    public Integer updatePayStatus( String orderId);
	    

		 /**
		  * 确认退款，修改状态
		  * @param orderId
		  * @return
		  */
	   @Modifying
	   @Transactional
	   @Query("update MemberAgent set  status = 3,payStatus =2  where orderId = ?1 and payStatus=1 ")
	   public Integer updateRefundStatus( String orderId);
	   //全加
	   @Modifying
	   @Transactional
	   @Query("update MemberAgent set  allmoney = allmoney + ?1 , curmoney = curmoney + ?1 where area = ?2 ")
	   public Integer addMoney(Double money,String area);
		//全减
	   @Modifying
	   @Transactional
	   @Query("update MemberAgent set  allmoney = allmoney - ?1 , curmoney = curmoney - ?1 where area = ?2 ")
	   public Integer decMoney(Double money,String area);


	@Transactional
	@Query(value="SELECT IFNULL(SUM(agent_money),0)  FROM `member_agent` WHERE pay_status =1 ",nativeQuery=true)
	public Double getMemberAgentSum();
	 
	@Transactional
	@Query(value="SELECT count(id)  FROM `member_agent` WHERE pay_status =1 ",nativeQuery=true)
	public int getMemberAgentCount();
	  
}